#!/bin/sh

timestamp=`date +%F-%H%M%S`
remote_app_ip=$2
local_app_ip=`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |cut -d: -f2 | awk '{ print $1}' | tail -n 1`
remote_app_dir=$3
local_app_dir=$1

foreachd(){

for f in  $1/* ;do

	if [ -d $f ];then

	    	remote_f=${remote_app_dir}/`echo $f | cut -d \/ -f5- ` #替换路径为远程主机路径例如 除/u02/applications/test1/ 后的路径
		remotefolder=`ssh ${remote_app_ip} "if [ -d $remote_f ];then echo "rsync folder sucess ${remote_app_ip}  $remote_f" ;else echo "rsync folder faild ${remote_app_ip}  $remote_f"; fi"`
		echo "$timestamp $remotefolder"	
	
		foreachd $f
       
	else
		
		remote_f=${remote_app_dir}/`echo $f | cut -d \/ -f5-`
		remotefile=`ssh ${remote_app_ip} "if [ -f $remote_f ];then md5sum $remote_f ;else echo "rsync file faild ${remote_app_ip}  $remote_f"; fi"`
        	localfile=`md5sum $f`
		
		if echo $remotefile | grep -q faild;then
		
			echo "$timestamp $remotefile"
		
		else
		
			remd5=`echo $remotefile | awk '{print $1}'`
			lomd5=`echo $localfile | awk '{print $1}'`
		
			if [ ${remd5} == ${lomd5} ];then
		
				md5status=sucess
		
			else
		
				md5status=faild
		
			fi
	
		fi

	        echo "$timestamp rsync file $md5status ${local_app_ip} $localfile  ${remote_app_ip} $remotefile"
		
	fi

done

}

foreachd $1 
